﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QUT.Bio.Util {
	public partial class PopupButton : UserControl {
		//--------------------- Types ----------------------------------------------
	
		public delegate void TextClickEventHandler( PopupButton source );
		
		//--------------------- Variables ------------------------------------------
		
		private const double inset = 5;
		private Canvas container;
		public event TextClickEventHandler TextClicked;
		
		//--------------------- Constructor ----------------------------------------
		
		public PopupButton() {
			InitializeComponent();
			SizeChanged += OnSizeChanged;
			textBlock.FontSize = 10;
			textBlock.TextDecorations = TextDecorations.Underline;
			textBlock.Cursor = Cursors.Hand;
			textBlock.MouseLeftButtonDown += OnTextClicked;
		}
		
		//--------------------- Properties -----------------------------------------
		
		public Border Border {
			get {
				return border;
			}
		}

		public TextBlock TextBlock {
			get {
				return textBlock;
			}
		}
		
		public Canvas Container {
			get {
				return container;
			}
			set {
				if ( value != container ) {
					if ( container != null ) {
						container.Children.Remove( this );
					}
					
					container = value;
					
					if ( container != null ) {
						container.Children.Add( this );
						EnsureFit();
					}
				}
			}
		}

		public double Left {
			get {
				return (double) GetValue( Canvas.LeftProperty );
			}
			set {
				SetValue( Canvas.LeftProperty, value );
			}
		}

		public double Top {
			get {
				return (double) GetValue( Canvas.TopProperty );
			}
			set {
				SetValue( Canvas.TopProperty, value );
			}
		}
		
		//--------------------- Methods --------------------------------------------
		
		void EnsureFit() {
			if ( container == null ) {
				return;
			}
					
			double left = (double) GetValue( Canvas.LeftProperty );
			double top = (double) GetValue( Canvas.TopProperty );

			if ( left >= container.ActualWidth - inset - ActualWidth ) {
				left = container.ActualWidth - inset - ActualWidth - 1;
			}

			if ( top >= container.ActualHeight - inset - ActualHeight ) {
				top = container.ActualHeight - inset - ActualHeight - 1;
			}
			 
			if ( left < inset ) {
				left = inset;
			}
			
			if ( top < inset ) {
				top = inset;
			}

			SetValue( Canvas.LeftProperty, left );
			SetValue( Canvas.TopProperty, top );
		}

		void OnSizeChanged( object sender, SizeChangedEventArgs e ) {
			EnsureFit();
		}
		
		void OnTextClicked ( object sender, MouseButtonEventArgs e ) {
			if ( TextClicked != null ) {
				TextClicked( this );
			}
		}
	}
}
